/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewAirportDialog.java
 *
 * Created on 8.12.2011, 13:33:20
 */
package view;

import data.model.Airport;
import data.model.Place;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import logic.business.DataEditor;
import logic.business.exceptions.MalformedDataException;

/**
 *
 * @author zhenia
 */
public class NewAirportDialog extends javax.swing.JDialog {

    private boolean result;
    private DataEditor dataEditor;
    List<Place> plac;
    private String[] placesName;

    /** Creates new form NewAirportDialog */
    public NewAirportDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        myInit();

    }

    NewAirportDialog(DataEditor de) {
        dataEditor = de;
        plac = de.getAllPlaces();
        placesName = new String[plac.size()];
        for (int i = 0; i < placesName.length; i++) {
            System.out.println(plac.get(i).getName());
            placesName[i] = plac.get(i).getName();
        }
        myInit();
    }

    private void myInit() {
        initComponents();
        places.setModel(new javax.swing.DefaultComboBoxModel(placesName));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        capacity = new javax.swing.JTextField();
        name = new javax.swing.JTextField();
        capacityLabel = new javax.swing.JLabel();
        nameLabel = new javax.swing.JLabel();
        places = new javax.swing.JComboBox();
        placeLabel = new javax.swing.JLabel();
        cancel = new javax.swing.JButton();
        ok = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("New Airport");

        capacity.setName("capacity"); // NOI18N

        name.setName("name"); // NOI18N
        name.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nameActionPerformed(evt);
            }
        });

        capacityLabel.setText("Transported passengers / year");
        capacityLabel.setName("capacityLabel"); // NOI18N

        nameLabel.setText("Name");
        nameLabel.setName("nameLabel"); // NOI18N

        places.setName("places"); // NOI18N
        places.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                placesActionPerformed(evt);
            }
        });

        placeLabel.setText("Place");
        placeLabel.setName("placeLabel"); // NOI18N

        cancel.setText("Cancel");
        cancel.setName("cancel"); // NOI18N
        cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });

        ok.setText("OK");
        ok.setName("ok"); // NOI18N
        ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(capacityLabel)
                    .addComponent(nameLabel)
                    .addComponent(placeLabel))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(name, javax.swing.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)
                    .addComponent(capacity)
                    .addComponent(places, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(142, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(262, Short.MAX_VALUE)
                .addComponent(ok)
                .addGap(18, 18, 18)
                .addComponent(cancel)
                .addGap(10, 10, 10))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameLabel)
                    .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(capacityLabel)
                    .addComponent(capacity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(placeLabel)
                    .addComponent(places, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(173, 173, 173)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ok)
                    .addComponent(cancel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void nameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nameActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_nameActionPerformed

    private void placesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_placesActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_placesActionPerformed

    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed

        // this.result = false;
        this.dispose();
        // TODO add your handling code here:
}//GEN-LAST:event_cancelActionPerformed

    private void okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okActionPerformed
        String placeName = places.getSelectedItem().toString();
        String id = name.getText() + "_" + placeName.toLowerCase();
        /* Place found;
        for (Place p : plac) {
        if (p.getName().equals(name)) {
        found = p;
        }
        }*/
        try {
            if (name.getText().isEmpty() || capacity.getText().isEmpty()) {
                JOptionPane.showMessageDialog(this,
                        "Airport name and number of passengers can't be null",
                        "Warning",
                        JOptionPane.WARNING_MESSAGE);
            }
            else if(Integer.parseInt(capacity.getText()) < 0){
                JOptionPane.showMessageDialog(this,
                        "Annual number of passengers must be positive integer",
                        "Warning",
                        JOptionPane.WARNING_MESSAGE);
            }
            else {
                dataEditor.addNewAirportToPlace(placeName, id, name.getText(), Integer.parseInt(capacity.getText()));
                this.result = true;
                this.dispose();
            }
        } catch (MalformedDataException ex) {
            JOptionPane.showMessageDialog(this,
                    "Airport name can't be null",
                    "Warning",
                    JOptionPane.WARNING_MESSAGE);
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this,
                    "Annual number of passengers must be positive integer",
                    "Warning",
                    JOptionPane.WARNING_MESSAGE);
        }


}//GEN-LAST:event_okActionPerformed

    boolean execute() {
        this.setVisible(true); // прорисовать дочернее окно; в данном месте выполнение программы приостановится, ожидая окончания работы пользователя (в нашем случае - функции dispose())
        return this.result;
    }
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancel;
    private javax.swing.JTextField capacity;
    private javax.swing.JLabel capacityLabel;
    private javax.swing.JTextField name;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JButton ok;
    private javax.swing.JLabel placeLabel;
    private javax.swing.JComboBox places;
    // End of variables declaration//GEN-END:variables
}
